
//状态
// function formatterState(value,row,index) {
//     var state
//     if(value == 0){
//         state = true;
//     }else {
//         state = false;
//     }
//     var sp = "<input class='easyui-switchbutton'/>";
//
//     return sp;
// }
function formatterState(value,row,index) {



    return value == 0 ? "<span style='color:green'>"+"启用"+"</span>" :"<span style='color: red'>"+"停用"+"</span>";
}

$(function () {
    var depot_datagrid = $("#depot_datagrid");
    var depot_add_dialog = $("#depot_add_dialog");
    var depot_add_form = $("#depot_add_form")
    var depot_add_save = $("#depot_add_save");

    depot_datagrid.datagrid({
        url:"/depot/list",
        fit:true,
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:"name",title:"仓库名称",width:100},
            {field:"sn",title:"编码",width:100},
            {field:"contacts",title:"联系人",width:100},
            {field:"tel",title:"联系电话",width:100},
            {field:"address",title:"地址",width:100},
            {field:"tel",title:"联系电话",width:100},
            {field:"state",title:"状态",width:100,formatter:formatterState},
            {field:"inputTime",title:"创建时间",width:100},
        ]],
        toolbar:"#depot_datagrid_toolbar",
        onLoadSuccess:function (data) {
            console.log(data);
        }
    })
    
    cmdObj = {
        //新增
        depot_add:function () {
            depot_add_form.form("clear")
            depot_add_dialog.dialog("open");
        },
        //修改
        depot_edit:function () {
            depot_add_dialog.dialog("setTitle","修改供应商")
            var selectedRow = depot_datagrid.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("温馨提醒","请先选中行","error");
                return;
            }
            depot_add_form.form("load",selectedRow)
            depot_add_dialog.dialog("open")
        },
        //删除
        depot_delete:function () {
            var selectedRow = depot_datagrid.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("温馨提醒","请先选中行","error");
                return;
            }
            $.messager.alert("温馨提醒","确定要删除吗","info",function () {

                $.post("/depot/delete",{id:selectedRow.id},function (data) {
                    if(data.success){
                        $.messager.alert("温馨提醒","删除成功","info",function () {
                            depot_datagrid.datagrid("reload");
                            depot_add_dialog.dialog("close")
                        })
                    }else{
                        $.messager.alert("温馨提醒",data.msg,"error")
                    }
                })
            })
        },
        depot_add_save:function () {
            var url = "depot/save";
            var idValue = $("#depot_add_form :input[name='id']").val();
            console.log(idValue);
            //判断是编辑还是回显
            if (idValue){
                url = "/depot/update";
            }

            depot_add_form.form("submit",{
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if (data.success){
                        $.messager.alert("温馨提醒","保存成功","info",function () {
                            depot_datagrid.datagrid("reload");
                            depot_add_form.dialog("close")
                        })
                    }else{
                        $.messager.alert("温馨提醒",data.msg,"error")
                    }
                }
            })
        },
        //刷新
        depot_reload:function () {
            depot_datagrid.datagrid("reload");
        }
        
    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]()
    })
})